package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author lhj
 * @Date 2024 - 05 - 28
 * @Description
 */
@Component
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {
    @Override
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
        return new Converter<String, T>() {
            @Override
            public T convert(String code) {
                T[] enumConstants = targetType.getEnumConstants();
                return Arrays.stream(enumConstants)
                        .filter(type -> code.equals(type.getCode() + ""))
                        .findFirst()
                        .orElseThrow(IllegalArgumentException::new);

//                for (T enumConstant : enumConstants) {
//                    if (enumConstant.getCode().equals(Integer.valueOf(code))){
//                        return enumConstant;
//                    }
//                }
//                throw new IllegalArgumentException("参数非法");
            }
        };
    }
}
